package com.dwms.controller;

import com.dwms.entity.ListRequest;
import com.dwms.entity.PageResponse;
import com.dwms.entity.QueryRequest;
import com.dwms.entity.Result;
import com.dwms.entity.declare.jeOutbound.request.JeOutboundImportRequest;
import com.dwms.entity.oms.order.request.HsReceiptImportRequest;
import com.dwms.entity.oms.order.request.OrderQueryRequest;
import com.dwms.entity.oms.order.response.OrderResponse;
import com.dwms.feign.oms.OrderFeignClient;
import com.dwms.utils.EmptyUtils;
import com.dwms.utils.ExcelImportUtil;
import com.dwms.utils.WebUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: OrderController
 * @Description: Order
 * @Create 2025-09-10 17:29:49
 * @Author WH
 * @Since 1.0.0
**/
@Tag(name = "平台订单")
@RestController(value = "OrderController")
@RequestMapping(value = "/plat/order")
@RequiredArgsConstructor
public class OrderController {

    private final HttpServletRequest request;
    private final OrderFeignClient orderFeignClient;

    /**
     * 分页查询
     *
     * @param query
     *          查询参数
     * @return
     */
    @PostMapping("/findPage")
    public Result<PageResponse<OrderResponse>> findPage(@RequestBody OrderQueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return orderFeignClient.findPage(query);

    }

    /**
     *  根据ID查询
     *
     * @param query
     *          参数
     * @return
     */
    @PostMapping("/findById")
    public Result findById(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request, query);
        return this.orderFeignClient.findById(query);
    }

    /**
     * 获取运单信息
     * @param query
     * @return
     */
    @PostMapping("/getWaybill")
    public Result getWaybill(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request, query);
        return this.orderFeignClient.getWaybill(query);
    }

    /**
     * 導出
     * @param query
     * @return
     */
    @PostMapping("/export")
    public Result<String> export(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return orderFeignClient.export(query);
    }

    /**
     * 批量申报
     * @param query
     * @return
     */
    @PostMapping("/batchDeclare")
    public Result batchDeclare(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return orderFeignClient.batchDeclare(query);
    }

    /**
     * 导入海关回执
     * @param file
     * @return
     */
    @PostMapping("/importHsReceipt")
    public Result importHsReceipt(@RequestParam(name = "file") MultipartFile file) {
        try {
            String[] headers = {"订单编号", "预录入编号", "清单编号"};
            String[] fields = {"orderSn", "preNo","invtNo"};
            List<HsReceiptImportRequest> hsReceiptImportRequests = ExcelImportUtil.excelToBeanUnderHead(file.getInputStream(), headers, fields, HsReceiptImportRequest.class);
            if (EmptyUtils.isEmpty(hsReceiptImportRequests)) {
                return Result.failed("数据读取失败");
            }
            if (hsReceiptImportRequests.size() > 10000) {
                return Result.failed("导入数据超过10000条,请分批次导入.");
            }
            ListRequest<HsReceiptImportRequest> listRequest = new ListRequest<>();
            listRequest.setList(hsReceiptImportRequests);
            WebUtil.getAdminRequest(request, listRequest);
            return this.orderFeignClient.importHsReceipt(listRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("导入出区数据失败" + e.getMessage());
        }
    }
}
